package mars.mp3player.xml;

import java.util.List;

import mars.mp3player.entity.Mp3Bean;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Mp3ListContentHandler extends DefaultHandler {
	private String tagName;
	private Mp3Bean mp3Bean;
	private List<Mp3Bean> mp3List;
	
	public Mp3ListContentHandler(List<Mp3Bean> mp3List) {
		this.mp3List = mp3List;
	}
	
	public List<Mp3Bean> getMp3List(){
		return this.mp3List;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String temp=new String(ch,start,length);
		if(tagName.equals("id")){
			mp3Bean.setId(temp);
		}else if(tagName.equals("mp3.name")){
			mp3Bean.setMp3Name(temp);
		}else if(tagName.equals("mp3.size")){
			mp3Bean.setMp3Size(temp);
		}else if(tagName.equals("lrc.name")){
			mp3Bean.setLrcName(temp);
		}else if(tagName.equals("lrc.size")){
			mp3Bean.setLrcSize(temp);
		}
	}

	@Override
	public void endDocument() throws SAXException {

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if(localName.equals("resource")){
			mp3List.add(mp3Bean);
		}
		tagName="";
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		this.tagName=localName;
		if(tagName.equals("resource")){
			mp3Bean=new Mp3Bean();
		}
	}
	
}
